package org.oschina.api.user.active.service;

import java.util.List;
import org.oschina.Config;
import org.oschina.Constants;
import org.oschina.api.BaseService;
import org.oschina.api.index.service.IndexService;
import org.oschina.api.manage.config.service.ConfigService;
import org.oschina.api.user.active.dao.UserActiveDAO;
import org.oschina.api.user.active.model.UserActive;
import org.oschina.api.user.active.vo.UserActiveVo;
import org.oschina.api.user.user.service.UserService;
import org.oschina.api.user.user.vo.UserVo;
import org.oschina.bean.Pager;
import org.oschina.security.annotation.Require;
import org.oschina.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserActiveService extends BaseService {

	@Autowired
	private UserActiveDAO userActiveDAO;
	@Autowired
	private IndexService indexService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private UserService userService;
	
	/**
	 * 根据作者返回
	 * @param author
	 * @param pn
	 * @return
	 */
	public Pager<UserActiveVo> findByAuthor(int author,int pn)
	{
		Pager<UserActiveVo> pager=new Pager(pn,0,_getPageCount(),null);
		UserVo userVo=userService.getVo(getToken());
		if(userVo!=null && userVo.getId().equals(author))
		{
			pager.setData_sum(_getCountByAuthorAndFollow(author));
			if(pager.getData_sum()>0)
				pager.setData(_findVoByAuthorAndFollow(author, pager.getStart(), pager.getPage_count()));
		}
		else
		{
			pager.setData_sum(_getCountByAuthor(author));
			if(pager.getData_sum()>0)
				pager.setData(findVoByAuthor(author, pager.getStart(), pager.getPage_count()));
		}
		return pager;
	}
	
	/**
	 * 创建
	 * @param major
	 * @param title
	 * @param content
	 * @param type
	 */
	@Require
	public void create(int major,String title,String content,int type)
	{
		UserVo author=userService.getVo(getToken());
		UserActive userActive=new UserActive();
		userActive.setAuthor(author);
		userActive.setMajor(major);
		userActive.setContent(content);
		userActive.setTitle(title);
		userActive.setType(type);
		_create(userActive);
	}
	
	/**
	 * 根据作者返回
	 * @param author
	 * @param first
	 * @param max
	 * @return
	 */
	private List<UserActiveVo> findVoByAuthor(int author,int first,int max)
	{
		return userActiveDAO.findVoByAuthor(author, first, max);
	}
	
	/**
	 * 根据作者返回数量
	 * @param author
	 * @return
	 */
	private int _getCountByAuthor(int author)
	{
		return userActiveDAO.getCountByAuthor(author);
	}
	
	/**
	 * 根据作者和关注返回
	 * @param author
	 * @param first
	 * @param max
	 * @return
	 */
	private List<UserActiveVo> _findVoByAuthorAndFollow(int author,int first,int max)
	{
		return userActiveDAO.findVoByAuthorAndFollow(author, first, max);
	}
	
	/**
	 * 根据作者和关注返回数量
	 * @param author
	 * @return
	 */
	private int _getCountByAuthorAndFollow(int author)
	{
		return userActiveDAO.getCountByAuthorAndFollow(author);
	}
	
	/**
	 * 创建
	 * @param userActive
	 * @return
	 */
	private UserActive _create(UserActive userActive)
	{
		userActive.setId(_getIndex());
		userActive.setCreate_time(DateUtil.now());
		return userActiveDAO.create(userActive);
	}

	private int _getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_USER_ACTIVE);
	}
	
	private int _getPageCount()
	{
		return (Integer)configService.getValueById(Config.CONFIG_19, Integer.class);
	}
}